package as;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Product;

public class Main2Serv extends HttpServlet {
	private static final long serialVersionUID = 1L;

	List<Product> productList;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main2Serv() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String productId = request.getParameter("productId");

		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/mysql";
		String user = "root";
		String password = "85428133";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, user, password);
			//Statement stmt = conn.createStatement();
			// String sql = "select * from product where ProductKbnA ='" + kind + "'";
			//ResultSet rs = stmt.executeQuery(sql);


			String sql = "select * from product where ProductId = ? ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, productId);

			ResultSet rs = pstmt.executeQuery();

            // ���������f�[�^�� �z�� class �ɃZ�b�g
			productList = new ArrayList<Product>();
//			List<Product>

            while(rs.next()) {
            	Product product = new Product();
            	product.setProductId(rs.getString("ProductId"));
            	product.setProductName(rs.getString("ProductName"));
            	product.setProductKbnＡ(rs.getString("ProductKbnＡ"));
            	product.setProductKbnＢ(rs.getString("ProductKbnＢ"));
            	product.setProductKbnＣ(rs.getString("ProductKbnＣ"));
            	product.setCostPrice(rs.getString("CostPrice"));
            	product.setSellingPrice(rs.getString("SellingPrice"));
            	product.setMaker(rs.getString("Maker"));
            	product.setSupplier(rs.getString("Supplier"));
            	product.setDummy１(rs.getString("Dummy１"));

            	productList.add(product);
            }

			rs.close();
//			stmt.close();
			pstmt.close();
//			conn.close();

		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (SQLException e) {
			System.out.println("Sql exception");
		} catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}

		request.setAttribute("products", productList);

		ServletContext context = this.getServletContext();
		RequestDispatcher dispatcher = context.getRequestDispatcher("/jsp/main3.jsp");
		dispatcher.forward(request, response);

	}

}
